//
//  MSPersonDatumViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/18.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSPersonDatumViewController.h"
#import "MSPersonDatumTableVC.h"
#import "MSUploadCardViewController.h"

@interface MSPersonDatumViewController ()
//侧边view
@property (nonatomic, strong) UIImageView *sideView;

@property (nonatomic, strong) MSPersonDatumTableVC *tableVC;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) DOImageView *icon;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *positionLabel;

@property (nonatomic, strong) UILabel *companyLabel;
@property (nonatomic, strong) UILabel *addressLabel;
@property (nonatomic, strong) UILabel *phoneLabel;

@property (nonatomic, strong) UIActionSheet       *uploadPhotoActionSheet;
@end

@implementation MSPersonDatumViewController

- (void)initNavigationBar{
    [super initNavigationBar];
    leftBarBtn.hidden = NO;
    titleLable.text = @"我的资料";
    
    rightBarBtn.hidden = NO;
    [rightBarBtn setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    [rightBarBtn addTarget:self action:@selector(rightBarBtnAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)rightBarBtnAction:(GXButton*)btn{
    btn.selected = !btn.selected;
    if (btn.selected) {
        if (!_sideView) {
            _sideView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-85, 70, 80, 44)];
            UIImage *newImage = [[UIImage imageNamed:@"pop_bg"] stretchableImageWithLeftCapWidth:0 topCapHeight:35];
            _sideView.image = newImage;
            _sideView.userInteractionEnabled = YES;
            _sideView.clipsToBounds = YES;
            [self.view addSubview:_sideView];
            NSArray *titleArray = @[@"更新名片"];
            for (int i =0; i<1; i++) {
                GXButton *sideButton = [GXButton buttonWithType:UIButtonTypeCustom];
                [sideButton setFrame:CGRectMake(0, 4+i*44, 80, 40)];
                sideButton.tag = 10+i;
                sideButton.titleLabel.font = HeitiFont(15);
                [sideButton setTitle:titleArray[i] forState:UIControlStateNormal];
                [sideButton addTarget:self action:@selector(sideButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                [_sideView addSubview:sideButton];
            }
            
        }
        _sideView.hidden = NO;
    }
    else{
        _sideView.hidden = YES;
    }
}
- (void)sideButtonAction:(GXButton*)btn{
    _sideView.hidden = YES;
    rightBarBtn.selected = NO;
    MSUploadCardViewController *vc = [[MSUploadCardViewController alloc] init];
    [rootNavigation pushViewController:vc animated:YES];
}

- (void)initUI{
    self.contentView.backgroundColor = kDefaultViewBackgroundColor;
    
    self.tableVC = [[MSPersonDatumTableVC alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame = CGRectMake(12, 12, SCREEN_WIDTH-24, 265);
    self.tableVC.enableRefresh = NO;
    self.tableVC.enableRequest = NO;
    self.tableVC.delegate = (id)self;
    self.tableVC.view.layer.cornerRadius = 6;
    self.tableVC.view.layer.borderColor = kNormalGray.CGColor;
    self.tableVC.view.layer.borderWidth = 0.6;
    self.tableVC.tableView.scrollEnabled = NO;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableVC.tableView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.tableVC.view];
    
    [self layoutHeaderView];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
    [self.contentView addGestureRecognizer:tapGesture];
}

- (void)layoutHeaderView{
    _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-24, 460)];
    self.tableVC.tableView.tableHeaderView = _headerView;
    
    self.icon = [[DOImageView alloc] initWithFrame:CGRectMake(16, 20, 60, 60)];
//    self.icon.imgURLStr = @"logo60";
    self.icon.layer.cornerRadius = 30;
    self.icon.clipsToBounds = YES;
    self.icon.userInteractionEnabled = YES;
    [_headerView addSubview:self.icon];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerClick)];
    [self.icon addGestureRecognizer:tapGesture];

    self.nameLabel = [self creatLabel:CGRectMake(86, 28, SCREEN_WIDTH-90, 20)];
    self.nameLabel.font = HeitiFont(15);
    self.positionLabel = [self creatLabel:CGRectMake(86, 52, SCREEN_WIDTH-90, 20)];
    self.positionLabel.textColor = kDarkGrayColor;

    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.icon.bottom+20, 4, 14)];
    lineLabel.backgroundColor = kBlueColor;
    [_headerView addSubview:lineLabel];
    
    self.companyLabel = [self creatLabel:CGRectMake(16, lineLabel.top-1, SCREEN_WIDTH-40, 16)];
    self.addressLabel = [self creatLabel:CGRectMake(16, self.companyLabel.bottom, SCREEN_WIDTH-40, 50)];
    self.addressLabel.numberOfLines = 0;
    self.addressLabel.textColor = kDarkGrayColor;

    UILabel *lineLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, lineLabel.top+85, 4, 14)];
    lineLabel2.backgroundColor = kBlueColor;
    [_headerView addSubview:lineLabel2];

    self.phoneLabel = [self creatLabel:CGRectMake(16, lineLabel2.top-6.5, SCREEN_WIDTH-40, 80)];
    self.phoneLabel.numberOfLines = 0;
}

//创建label
- (UILabel*)creatLabel:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = HeitiFont(13);
    label.textColor = [UIColor blackColor];
    [_headerView addSubview:label];
    return label;
}

- (void)testDataFunc{
    MSUserInfoModel *model = [[MSUserInfoModel alloc] init];
    model.headIcon = @"";
    model.name = @"王振华";
    model.position= @"总经理";
    model.area =  @"华东";
    model.department =  @"电子与能源事业部";
    model.company =  @"中国有限公司";
    model.address= @"上海市浦东新区张江高科人太多和任何瑞特让他有呢如同不让他";
    model.societyNum =  @"342322";
    model.telephone =  @"18734384739";
    model.email =  @"23423423@126.com";
    model.internet= @"www.3m.com.cn";
    
//    [self loadPersonInfoWith:model];
    [netEngine requestUserInfoWithTarget:self.VctrID hudView:self.contentView];
}

- (void)updateViewController:(NSNotification *)note {
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"UserInfo"]!=nil){
            MSUserInfoModel *userModel = responseDic[@"UserInfo"];
            [self loadPersonInfoWith:userModel];
        }
        if (responseDic[@"UploadImage"]!=nil){
            NSMutableArray *imageId = [NSMutableArray arrayWithCapacity:0];
            NSArray *pictureArray = responseDic[@"UploadImage"];
            for (propertyPicture *pictureModel in pictureArray) {
                [imageId addObject:[HDUtil check:pictureModel.cateId]];
            }
            NSString *imageStr = [imageId componentsJoinedByString:@","];
            
//            NSData *data = [NSJSONSerialization dataWithJSONObject:@{@"portraitImgId":imageStr} options:NSJSONWritingPrettyPrinted error:nil];
//            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            [netEngine requestModifyHeadImageWithTarget:self.VctrID hudView:self.contentView params:imageStr];
        }
        if (responseDic[@"ModifyHeadImage"]!=nil){
            
        }
    }
}

- (void)loadPersonInfoWith:(MSUserInfoModel *)model{
//    self.icon.defaultImg = @"logo60";
    if (model.headIcon.length) {
        self.icon.imgURLStr = model.headIcon;
    }
    else{
        self.icon.defaultImg = @"logo60";
    }    self.nameLabel.text = model.name;
    self.positionLabel.text = [NSString stringWithFormat:@"%@",model.position];
//    self.positionLabel.text = [NSString stringWithFormat:@"%@  (%@)",model.position,model.area];
    NSMutableAttributedString *departmentAttStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@   ",model.department] attributes:@{NSForegroundColorAttributeName:kDarkGrayColor}];
    NSAttributedString *company = [[NSAttributedString alloc] initWithString:model.company];
    [departmentAttStr appendAttributedString:company];
    
    self.companyLabel.attributedText = departmentAttStr;
    self.addressLabel.text = model.address;
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 8;
    
    NSMutableAttributedString *societyAttStr = [[NSMutableAttributedString alloc] initWithString:@"江湖号: "  attributes:@{NSForegroundColorAttributeName:kLightGrayColor,NSParagraphStyleAttributeName:paragraphStyle}];
    NSAttributedString *societyValue = [[NSAttributedString alloc] initWithString:model.societyNum];
    NSAttributedString *phone = [[NSAttributedString alloc] initWithString:@"       手机号: " attributes:@{NSForegroundColorAttributeName:kLightGrayColor}];
    NSAttributedString *phoneValue = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n",model.telephone]];
    NSAttributedString *email = [[NSAttributedString alloc] initWithString:@"电子邮件: " attributes:@{NSForegroundColorAttributeName:kLightGrayColor,NSParagraphStyleAttributeName:paragraphStyle}];
    NSAttributedString *emailValue = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n",model.email]];
    NSAttributedString *internet = [[NSAttributedString alloc] initWithString:@"公司网址: " attributes:@{NSForegroundColorAttributeName:kLightGrayColor}];
    NSAttributedString *internetValue = [[NSAttributedString alloc] initWithString:model.internet];
    
    [societyAttStr appendAttributedString:societyValue];
    [societyAttStr appendAttributedString:phone];
    [societyAttStr appendAttributedString:phoneValue];
    [societyAttStr appendAttributedString:email];
    [societyAttStr appendAttributedString:emailValue];
    [societyAttStr appendAttributedString:internet];
    [societyAttStr appendAttributedString:internetValue];
    
    self.phoneLabel.attributedText = societyAttStr;
}

- (void)headerClick{
    [self createActionSheet];
}

#pragma mark - actionSheet
- (void)createActionSheet {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        self.uploadPhotoActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:(id)self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍摄",@"从相册选择",nil];
    }else {
        self.uploadPhotoActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:(id)self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从手机相册选择", nil];
    }
    self.uploadPhotoActionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    self.uploadPhotoActionSheet.delegate = (id)self;
    [self.uploadPhotoActionSheet showInView:self.view];
}

#pragma mark -actionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == actionSheet.cancelButtonIndex) {
        
    }
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        switch (buttonIndex) {
            case 0:
            {
                [self takePhoto];
            }
                break;
            case 1:
            {
                [self openLocalAlbum];
            }
                break;
            default:
                break;
        }
    }
    else
    {
        switch (buttonIndex) {
            case 0:
            {
                [self openLocalAlbum];
            }
                break;
            default:
                break;
        }
    }
}

#pragma mark -takePhoto
- (void)takePhoto {
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = (id)self;
        imagePicker.allowsEditing = YES;
        imagePicker.sourceType = sourceType;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }else {
        NSLog(@"模拟器无法调用相机");
    }
}

#pragma mark - openLocalAlbum
- (void)openLocalAlbum {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = (id)self;
    imagePicker.allowsEditing = YES;
    [self presentViewController:imagePicker animated:YES completion:nil];
}

#pragma mark - pickerViewControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    //        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];//原始图片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];//裁剪后图片
//    [self reloadTableViewWithImage:image];
    self.icon.image = image;
    [self dismissViewControllerAnimated:YES completion:nil];
    [netEngine requestUploadImageWithTarget:self.VctrID hudView:self.contentView params:@[image]];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)tapClick{
    _sideView.hidden = YES;
    rightBarBtn.selected = NO;
}

@end
